package com.hylanda.processors.HNGetH2x.util;

import java.io.StringReader;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.alibaba.fastjson.JSONObject;


public class XMLUtil {
	
	public static JSONObject getJson(String xmlString)
	{
		JSONObject jsonObject = new JSONObject();
		
		
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(new StringReader(xmlString));
			Element root = document.getRootElement();
			//Element data = root.getChild("Node");
			/*Element TXTI = root.getChild("TXTI");
			String title = TXTI.getValue();
			System.out.println(title);*/
			
			
			//String tests = (root.getChild("test")==null)? null:root.getChild("text").getValue();
			//String testss = tests.getValue();
			//System.out.println(tests);
			String error = root.getChildText("error");
			if (error!=null&&!error.equals("")) {
				jsonObject.put("reson", "结构化失败");
				jsonObject.put("detail", error);
				jsonObject.put("msg", "fail");
				return jsonObject;
			}
			
			String page_url = root.getChildText("PAGEURL");
			String page_title = root.getChildText("PGTI");
			if (page_title!=null&&!page_title.equals("")) {
				page_title = page_title.replaceAll("\r|\n", " ");
			}
			String title = root.getChildText("TXTI");
			if (title!=null&&!title.equals("")) {
				title = title.replaceAll("\r|\n", " ");
			}
			String auth = root.getChildText("AUTH");
			String from = root.getChildText("FROM");
			String contentFrom = root.getChildText("CONTENTFROM");
			String date = root.getChildText("DATE");
			String format_content = root.getChildText("TEXT");
			if (format_content!=null&&!format_content.equals("")) {
				format_content = format_content.replaceAll("\r|\n", " ");
			}
			String content = root.getChildText("CONTENT");
			if (content!=null&&!content.equals("")) {
				content = content.replaceAll("\r|\n", " ");
			}else{
				jsonObject.put("result", "没有获取到正文");
				jsonObject.put("msg", "fail");
				return jsonObject;
			}
			String nav = root.getChildText("NAV");
			if (nav!=null&&!nav.equals("")) {
				nav = nav.replaceAll("\r|\n", " ");
			}
			String sabs = root.getChildText("SABS");
			if (sabs!=null&&!sabs.equals("")) {
				sabs = sabs.replaceAll("\r|\n", " ");
			}
			String keyword = root.getChildText("KWRD");
			if (keyword!=null&&!keyword.equals("")) {
				keyword = keyword.replaceAll("\r|\n", " ");
			}
			String class17 = root.getChildText("Class17");
			//String clazz = root.getChildText("CLASS");
			String tags = root.getChildText("TAG");
			if (tags!=null&&!tags.equals("")) {
				tags = tags.replaceAll("\r|\n", " ");
			}
			String download_time = root.getChildText("DOWNTIME");
			//String download_type = root.getChildText("DOWNTYPE");
			String picture_description = root.getChildText("PICTEXT");
			if (picture_description!=null&&!picture_description.equals("")) {
				picture_description = picture_description.replaceAll("\r|\n", " ");
			}
			String picture_list = root.getChildText("PICLIST");
			String title_crc = root.getChildText("TITLE_UUID");
			String content_crc = root.getChildText("UUID");
			String url_type = root.getChildText("URLTYPE");
			
			jsonObject.put("keywords", keyword);
			jsonObject.put("page_title", page_title);
			jsonObject.put("refer_source_type", "");
			jsonObject.put("original_media_url", "");
			jsonObject.put("title", title);
			jsonObject.put("content",  content);
			jsonObject.put("sensitive_words", "");
			jsonObject.put("navigation", nav);
			jsonObject.put("class23", "");
			jsonObject.put("picture_list", picture_list);
			jsonObject.put("title_crc", title_crc);
			jsonObject.put("media_name", from);
			jsonObject.put("download_date", download_time);
			jsonObject.put("content_crc", content_crc);
			jsonObject.put("lastmodify_date", "");
			jsonObject.put("original_media_name", contentFrom);
			jsonObject.put("author", auth);
			jsonObject.put("refer_url", "");
			jsonObject.put("source_type", url_type);
			jsonObject.put("abstract", sabs);
			jsonObject.put("url", page_url);
			jsonObject.put("tags", tags);
			jsonObject.put("picture_description", picture_description);
			jsonObject.put("release_date", date);
			jsonObject.put("format_content", format_content);
			jsonObject.put("rel_type", "");
			jsonObject.put("other_content", "");
			jsonObject.put("content_media_name", contentFrom);
			jsonObject.put("other_page", page_url);
			jsonObject.put("class17", class17);
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return jsonObject;
	}

}
